#ifndef __USER_MB_APP_H__
#define __USER_MB_APP_H__
/* ----------------------- Modbus includes ----------------------------------*/
#include "mb.h"
#include "mbconfig.h"
#include "mbframe.h"
#include "mbutils.h"

/* -----------------------Slave Defines -------------------------------------*/
#define S_DISCRETE_INPUT_START (10000)
#define S_DISCRETE_INPUT_NDISCRETES (24)

#define S_COIL_START (0)
#define S_COIL_NCOILS (8)

#define S_REG_INPUT_START (30000)
#define S_REG_INPUT_NREGS (24)

#define S_REG_HOLDING_START (40000)
#define S_REG_HOLDING_NREGS (20)
/*------------------------------------master-------------------------------------*/
typedef enum
{
    COIL_SET,
    COIL_CLEAR,
    COIL_TOGGLE,
} eCoilcmd;

// 将byte中的第n位设置为0
static inline void set_bit(unsigned char *byte, int n)
{
    *byte |= (1 << (n));
}

// 将byte中的第n位设置为0
static inline void clear_bit(unsigned char *byte, int n)
{
    *byte &= ~(1 << (n));
}

// 切换byte中的第n位的值（从0变1，从1变0）
static inline void toggle_bit(unsigned char *byte, int n)
{
    *byte ^= (1 << (n));
}

extern int32_t usSDiscInRegSet(uint16_t address, eCoilcmd cmd);

extern int32_t usSCoilRegGet(uint16_t address, uint8_t *data);
extern int32_t usSCoilRegSet(uint16_t address, eCoilcmd cmd);

extern int32_t usSRegInRegSet(uint16_t address, uint16_t *data);

extern int32_t usSRegHoldRegGet(uint16_t address, uint16_t *data);
extern int32_t usSRegHoldRegSet(uint16_t address, uint16_t *data);
#endif /* __USER_MB_APP_H__ */
